package com.mos.action;

import com.mos.base.sql.Page;
import com.mos.entity.User;
import com.mos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Package Name: com.mos.action
 * User: grq
 * Date: 2016/3/24
 * Time: 15:02
 * Description:some des here!
 */
@Controller
@RequestMapping("/user")
public class UserAction {

    public static final String PREFIX = "/user/";

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public String save(){
        User user = new User();
        user.setName("dave");
        user.setPassword("123456");
        userService.save(user);
        return PREFIX + "list";
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo") Integer pageNo,@RequestParam(value = "pageSize") Integer pageSize){

        Page<User> page = userService.queryPage(pageNo,pageSize,null);
        if (!page.getResultList().isEmpty()){
            List<User> users = page.getResultList();
            for (User user : users){
                System.out.println(user.getName()+user.getId());
            }
        }
        System.out.println("current page is :"+page.getCurrentPage());
        System.out.println("number per page is :"+page.getNumPerPage());
        System.out.println("last index is :"+page.getLastIndex());
        System.out.println("start index is :"+page.getStartIndex());
        System.out.println("total Rows is :"+page.getTotalRows());
        return PREFIX + "list";

    }
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public String find(@RequestParam(value = "id") Integer id){

        User user = userService.findById(id);
        System.out.println(user.getId());
        return PREFIX + "list";

    }

    @RequestMapping(value = "/findObject",method = RequestMethod.GET)
    public String findObject(@RequestParam(value = "id") Integer id){

        Map<String,Object> user = userService.find(id);
        System.out.println(user.get("name"));
        return PREFIX + "list";

    }

    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public String findList(@RequestParam(value = "name") String name){

       List<Map<String,Object>> users = userService.queryList(name);
        System.out.println(users.size());
        return PREFIX + "list";

    }
}
